<template>
  <el-dialog
    :title="title"
    :visible="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    center
  >
    <el-form
      ref="addEditForm"
      v-loading="isLoading > 0"
      :model="formData"
      :rules="rules"
      label-width="100px"
      class="add-edit-form"
    >
      <el-form-item v-if="formData.p_id" label="父权限id">
        <el-input v-model="formData.p_id" disabled />
      </el-form-item>
      <el-form-item label="权限名称" prop="auth_name">
        <el-input v-model="formData.auth_name" />
      </el-form-item>
      <el-form-item label="关键字（必须是英文）" prop="keywords">
        <el-input v-model="formData.keywords" />
      </el-form-item>
      <el-form-item label="是否上线">
        <el-switch
          :value="formData.status === 1"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="formData.status = formData.status === 1 ? -1 : 1"
        />
      </el-form-item>

      <el-form-item class="submit-box">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button v-if="!formData.id" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
const formData = {
  p_id: '',
  auth_name: '',
  keywords: '',
  status: -1
}
import _ from 'lodash'
import { addOneAuth, updateOneAuthById } from '@/api/auth'
export default {
  name: 'AddEditAuth',
  components: {},
  props: {},
  data() {
    return {
      dialogVisible: false,
      isLoading: 0,
      formData: {},
      row: null,
      rules: {
        auth_name: [
          { required: true, message: '不能为空', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  computed: {
    title() {
      let str = ''
      if (this.formData.id) {
        str += '编辑'
      } else {
        str += '添加'
      }
      if (this.formData.p_id) {
        str += '按钮'
      } else {
        str += '页面'
      }
      return str + '权限'
    }
  },
  watch: {},
  created() {
  },
  mounted() {},
  methods: {
    handleClose() {
      this.dialogVisible = false
    },
    add(p_id) {
      this.row = null
      this.dialogVisible = true
      this.formData = _.cloneDeep(formData)
      this.formData.p_id = p_id
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    edit(row) {
      this.row = row
      this.dialogVisible = true
      this.formData = _.cloneDeep(row)
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    resetForm() {
      if (this.row) {
        this.edit(this.row)
      } else {
        this.add()
      }
    },
    submitForm() {
      if (this.isLoading > 0) return
      this.$refs.addEditForm.validate(async vaild => {
        if (!vaild) return
        try {
          this.isLoading++
          if (this.formData.id) {
            await updateOneAuthById(this.formData)
          } else {
            await addOneAuth(this.formData)
          }
          this.$emit('refresh')
          this.handleClose()
        } finally {
          this.isLoading--
        }
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.submit-box {
  display: flex;
  justify-content: flex-end;
}
</style>
